import addEditModal from './music-type-components/add-edit-modal.vue';

export default {
    // name:'music-type',
    components:{
        addEditModal
    },
    computed:{
        user(){
            return this.$store.state.user.data;
        }
    },
    data(){
        return{
            musicTypeList:[],
            show:false,
            showEditModal:false,
            injectData:{},
            showCoverLoading:false
        }
    },
    methods:{
        init(){
            if(this.showCoverLoading) return
            this.showCoverLoading = true;
            this.http.post(this.postUrl.getMusicType,{
                token:this.user.tokenList[0],
                app_source:'xpqh'
            }).then((data)=>{
                console.log(data);
                if(data.code > -1){
                    let parentTypeList = [];
                    let childTypeList = [];
                    data.data.forEach(item => {
                        if (item.depth == 1){
                            parentTypeList.push(item)
                        }else{
                            childTypeList.push(item)
                        }
                    });
                    console.log(parentTypeList, childTypeList);
                    parentTypeList.forEach(parent => {
                        childTypeList.forEach(child => {
                            if (child.parent_uuid == parent.uuid){
                                if (!parent.children){
                                    parent.children = []
                                }
                                parent.children.push(child)
                            }

                        });
                    });
                    parentTypeList.forEach(parent => {
                        parent.show = false;
                        if (parent.children && parent.children.length){
                            parent.children.sort((a,b)=>{
                                return a.orders - b.orders
                            })

                        }
                    });
                    parentTypeList.sort((a, b) => {
                        return a.orders - b.orders
                    })
                    this.musicTypeList = parentTypeList;
                    console.log(parentTypeList);
                }else{
                    this.errorHandle(data.msg)
                }
            },()=>{
                this.errorHandle()
            }).then(()=>{
                this.showCoverLoading = false;

            })
        },
        clickAddBtn(item){
            this.injectData = item;
            this.showEditModal = true;
        },
        addNewTypeSuccess(){
            this.init()
        },
        clickEditBtn(item){
            item.edit = true;
            this.injectData = item;
            this.showEditModal = true;
        },
        deleteType(item,index,childIndex){
            this.$Modal.confirm({
                title: '提示',
                content: '<p>注意！您现在要删除它！</p>',
                'ok-text':'我确定',
                loading: true,
                onOk: ()=>{
                    this.http.post(this.postUrl.deleteMusicType,{
                        uuid: item.uuid,
                        token:this.user.tokenList[0]
                    }).then((data)=>{
                        if(data.code > -1){
                            this.$Notice.success({
                                title: '删除成功！',
                            });
                            if (childIndex === undefined){
                                this.musicTypeList.splice(index,1)
                            }else{
                                this.musicTypeList[index].children.splice(childIndex, 1)
                            }
                        }else{
                            this.errorHandle(data.msg)
                        }
                    },()=>{
                        this.errorHandle()
                    }).then(()=>{
                        this.$Modal.remove();
                    })
                }
            });
        },
        showChildren(item,index){
            if (this.musicTypeList[index].leaf_flag === '1'){
                return
            }
            this.musicTypeList[index].show = !this.musicTypeList[index].show;
        },
        sortList(nextItem,index,childIndex){
            console.log(nextItem, index, childIndex);
            this.showCoverLoading = true;
            let body = {}
            if (childIndex === undefined){
                body.move_uuid = this.musicTypeList[index].uuid;
                body.passive_move_uuid = this.musicTypeList[index + nextItem].uuid;
            }else{
                body.move_uuid = this.musicTypeList[index].children[childIndex].uuid;
                body.passive_move_uuid = this.musicTypeList[index].children[childIndex + nextItem].uuid;
            }
            body.token = this.user.tokenList[0]
            this.http.post(this.postUrl.orderMusucTypeList, body).then((data)=>{
                if(data.code > -1){
                    if (childIndex === undefined){
                        let cache = this.musicTypeList[index];
                        this.musicTypeList[index] = this.musicTypeList[index + nextItem]
                        this.musicTypeList[index + nextItem] = cache;
                    }else{
                        let childCache = this.musicTypeList[index].children[childIndex];
                        this.musicTypeList[index].children[childIndex] = this.musicTypeList[index].children[childIndex + nextItem]
                        this.musicTypeList[index].children[childIndex + nextItem] = childCache;
                    }
                }else{
                    this.errorHandle(data.msg)
                }
                this.showCoverLoading = false;
            },()=>{
                this.showCoverLoading = false;
                this.errorHandle()
            })
        }
    },
    mounted(){
        setTimeout(() => {
            this.init()
        }, 200);
    },
}
